<?php
/* explain:		access class
 * Project:     
 * File:        Access.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	Ivan  Ling
 * @ version 3.0
 *
 */
include_once("Common.php");

class AccessImpl extends Common
{
	function Access()
	{
		parent::Common();

	}
	
	/* write login log
	 * 
	 * @ param  string 		DBtag
	 * @ param  datetime 	pageid
	 * @ param  return 		id
	 */
	public function add($DBtag, $pageid)
	{
		global $_SERVER;
		
		$loginid	= 0;
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($DBtag);
			
			$sql 		= "INSERT INTO access_log SET pageid=$pageid, ip='" . $_SERVER['REMOTE_ADDR'] . "', urlFrom='".addslashes($_SERVER['HTTP_REFERER'])."', userAgent='".addslashes($_SERVER['HTTP_USER_AGENT'])."', visitTime=UTC_TIME(), visitDate=UTC_DATE()";
			$rs			= mysql_query($sql, $conn);// or die ('error='.mysql_error());  
			
			if($rs)
				$loginid 	= mysql_insert_id();
	 	}catch(Exception $e){
			echo mysql_error($conn) . $sql;
		}
		
		if($conn != null)
			$this->freeConnection($DBtag, $conn);
			
		return $loginid;
	}


	/* access log list
	 *
	 * @ param  string 	where   	a?? where condition
	 * @ param  string 	page   		a?? curent page
	 * @ param  string 	discount   	a?? display data count
	 * @ param  string 	url   		a?? user url
	 * @ param  return 	array		a?? data array
	 */
	public function lists($arrPopedom = array(), $DBtag, $where, $page = '', $discount = '', $url = '')
	{
		if($this->isUnallowable($arrPopedom, Popedom::STRING_VIEW))
			die ($this->getNotPopedomInfo());
		
		$arrback 	= array();
		$conn 		= null;
		
		try{
			$conn 	= $this->getConnection($DBtag);
	
			$sql 	= "SELECT count(id) FROM access_log $where";
			$rs  	= mysql_query($sql,$conn) or die(mysql_error());
			
			list($total) = mysql_fetch_row($rs);
			$this->mutilPager($total, $page, $discount, $url);
			if(!empty($total)){
				if(empty($discount))
					$sql = "SELECT * FROM access_log $where";
				else{
					$sql = "SELECT * FROM access_log $where LIMIT $this->firstcount, $discount";
				}
		
				$rs  = mysql_query($sql, $conn);
				if($rs){
					while(is_array($arrtmp = mysql_fetch_array($rs))){
						$arrback[] = $arrtmp;
					}
				}
			}
		}catch(Exception $e){
			echo mysql_error($conn) . $sql;
		}
		
		if($conn != null)
			$this->freeConnection($DBtag, $conn);
		
		return $arrback;
	}
	

	public function arrAnalyse()
	{
		global $_SERVER;
		
		$arrReturn = array();
		
		$osAndBrowse = $_SERVER['HTTP_USER_AGENT'];	// visitor os and browse

		//--> get browse
		if(strpos($osAndBrowse,'NetCaptor')){
			$arrReturn['browse'] = 'NetCaptor';
		}elseif($iStartPos = strpos($osAndBrowse,'MSIE')){
			$iEndPos = strpos($osAndBrowse, ";", $iStartPos);
			if($iEndPos){
				$arrReturn['browse'] = substr($osAndBrowse, $iStartPos, $iEndPos - $iStartPos);
			}
		}elseif(strpos($osAndBrowse,'Netscape')){
			$arrReturn['browse'] = "Netscape";
		}elseif(strpos($osAndBrowse,'Firefox')){
			$arrReturn['browse'] = trim(strstr($osAndBrowse, 'Firefox'));
		}elseif(strpos($osAndBrowse,'Opera')){
			$arrReturn['browse'] = 'Opera';
		}else{
			$arrReturn['browse'] = 'other';
		}


		//--> get OS
		if(strpos($osAndBrowse,'Windows NT 5.0'))
			$arrReturn['os'] = 'Windows2000';
		elseif(strpos($osAndBrowse,'Windows NT 5.1'))
			$arrReturn['os'] = 'WindowsXP';
		elseif(strpos($osAndBrowse,'Windows NT'))
			$arrReturn['os'] = 'WindowsNT';
		elseif(strpos($osAndBrowse,'Windows 9'))
			$arrReturn['os'] = 'Windows9x';
		elseif(strpos($osAndBrowse,'unix'))
			$arrReturn['os'] = 'unix';
		elseif(strpos($osAndBrowse,'linux'))
			$arrReturn['os'] = 'linux';
		elseif(strpos($osAndBrowse,'SunOS'))
			$arrReturn['os'] = 'SunOS';
		elseif(strpos($osAndBrowse,'BSD'))
			$arrReturn['os'] = 'FreeBSD';
		elseif(strpos($osAndBrowse,'Mac'))
			$arrReturn['os'] = 'Mac';
		else
			$$arrReturn['os'] = 'Other';
	
		//--> get IP
		$arrReturn['ip'] = $_SERVER['REMOTE_ADDR'];
		
		
		//--> from url
		if($_SERVER['HTTP_REFERER'] == '')
			$arrReturn['urlfrom'] = 'manual input';
		else
			$arrReturn['urlfrom'] = $_SERVER['HTTP_REFERER'];

		return $arrReturn;
	}
}
?>
